﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MMO_MANAGER.DTO;
using MMO_MANAGER.BUS;
using MMO_MANAGER.Utils;
using MMO_MANAGER.FORM_POPUP;
using System.Collections;

namespace MMO_MANAGER.FORM_CATALOG
{
    public partial class frmKeyWordList : DevExpress.XtraEditors.XtraForm
    {
        public frmKeyWordList()
        {
            InitializeComponent();
        }

        key_word_bus bus;
        key_word kw;
        string chuoi = "mahoa";
        string NotEdit = "Not Edit Data!";
        private void frmKeyWordList_Load(object sender, EventArgs e)
        {
            bus = new key_word_bus();
            kw = new key_word();
            FillData();
        }

        private void FillData()
        {
            gridControl1.DataSource = bus.GetData();
        }

        private void btn_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
            FillData();
        }

        private void btn_edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
            FillData();
        }

        private void btn_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
            FillData();
        }

        private void btn_refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FillData();
        }

        private void btn_help_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        #region Add Edit Delete

        void Add()
        {
            try
            {
                using (var frm = new KeyWordDlg())
                {
                    if (frm.ShowDialog(FindForm()) == DialogResult.OK)
                    {
                        kw.ten = frm.NameKW;
                        kw.tukhoa = frm.TuKhoa;
                        kw.ghichu = frm.Note;
                        kw.active = 1;    //  active
                        kw.ngayedit = DateTime.Now;

                        DataTable dt = bus.GetListKWByName(kw);
                        if (dt.Rows.Count == 0)
                        {
                            bus.Insert(kw);
                            EasyDialog.ShowSuccessfulDialog("Thêm thành công!");
                            //lbl_totalrows.Text = gridView1.RowCount.ToString();
                        }
                        else
                        {
                            EasyDialog.ShowWarningDialog("Đã có 'Keyword' này rồi!");
                            return;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        void Edit()
        {
            try
            {
                int row = gridView1.FocusedRowHandle;
                kw.id = Convert.ToInt16(gridView1.GetRowCellValue(row, "id"));
                kw.ten = gridView1.GetRowCellValue(row, "ten").ToString();
                kw.tukhoa = gridView1.GetRowCellValue(row, "tukhoa").ToString();
                kw.ghichu = gridView1.GetRowCellValue(row, "ghichu").ToString();

                using (var frm = new KeyWordDlg())
                {
                    //frm.txt_pass.ReadOnly = true;
                    frm.NameKW = kw.ten;
                    frm.TuKhoa = kw.tukhoa;
                    frm.Note = kw.ghichu;

                    if (frm.ShowDialog(FindForm()) == DialogResult.OK)
                    {
                        kw.ten = frm.NameKW;
                        kw.tukhoa = frm.TuKhoa;
                        kw.ghichu = frm.Note;
                        kw.ngayedit = DateTime.Now;
                        bus.Update(kw);
                        EasyDialog.ShowSuccessfulDialog("Sửa thành công!");
                    }
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        void Delete()
        {
            try
            {
                int row = gridView1.FocusedRowHandle;
                kw.id = Convert.ToInt16(gridView1.GetRowCellValue(row, "id"));
                DialogResult dr = EasyDialog.ShowYesNoDialog("Bạn có muốn xóa không?");
                if (dr == DialogResult.Yes)
                {
                    kw.active = 0;
                    bus.Delete(kw);
                    EasyDialog.ShowSuccessfulDialog("Xóa thành công!");
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        #endregion

        private void btn_show_Click(object sender, EventArgs e)
        {
            try
            {
                int row = gridView1.FocusedRowHandle;
                kw.tukhoa = gridView1.GetRowCellValue(row, "tukhoa").ToString();

                using (var frm = new frmMemo())
                {
                    frm.Text="Tag Keyword";
                    string[] lskey = kw.tukhoa.Split(',');
                    ArrayList array = new ArrayList();      // create array
                    foreach (var item in lskey)
                    {
                        array.Add(item.Trim());         // add to array
                    }
                    frm.txt_memo.Lines = array.ToArray(typeof(string)) as string[];

                    if (frm.ShowDialog(FindForm()) == DialogResult.OK)
                    {
                        Clipboard.SetText(frm.txt_memo.Text.Trim());
                    }
                }
            }
            catch (Exception ex)
            {
                EasyDialog.ShowErrorDialog("Lỗi: " + ex.Message);
                return;
                throw;
            }
        }

        private void btn_copy_Click(object sender, EventArgs e)
        {
            int row = gridView1.FocusedRowHandle;
            kw.tukhoa = gridView1.GetRowCellValue(row, "tukhoa").ToString();
            Clipboard.SetText(kw.tukhoa);
            EasyDialog.ShowSuccessfulDialog("Copy successfull!");
        }

    }
}